package com.situ.storehouse.listener;

import javax.servlet.ServletContext;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;


//监听器类，用于统计在线人数
@WebListener
public class OnlineListener implements HttpSessionListener {

	@Override
	public void sessionCreated(HttpSessionEvent se) {
		HttpSession session=se.getSession();
		ServletContext application=session.getServletContext();
		Object attribute=application.getAttribute("count");
		if(null==attribute) {
			application.setAttribute("count",1);
		}else {
			int count=(int)attribute;
			application.setAttribute("count", ++count);
		}
	}

	@Override
	public void sessionDestroyed(HttpSessionEvent se) {
		HttpSession session=se.getSession();
		ServletContext application=session.getServletContext();
		int count=(int)application.getAttribute("count");
		application.setAttribute("count", --count);
	}

	
	
	
}
